package com.mercury.user.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mercury.user.entity.CenterUserTasted;
import com.mercury.user.service.CenterUserTastedService;
import com.mercury.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tong
 * @since 2021-11-14
 */
@RestController
@RequestMapping("/user/center-user-tasted")
@CrossOrigin
public class CenterUserTastedController {
    @Autowired
    private CenterUserTastedService tastedService;

    /**
     * @return
     * @date 2021/11/15 10:03
     * @params userTasted
     * @description 标记用户喝过的咖啡
     */
    @ApiOperation("标记用户喝过的咖啡")
    @PostMapping("/signTastedCoffee")
    private R signTastedCoffee(@RequestBody CenterUserTasted userTasted) {
        boolean save = tastedService.save(userTasted);
        if (save) {
            return R.ok().message("添加成功");
        } else {
            return R.error().message("添加失败");
        }
    }


    /**
     * @return
     * @date 2021/11/15 10:04
     * @params userTasted
     * @description 取消标记用户喝过的咖啡
     */
    @ApiOperation("删除用户喝过的咖啡")
    @DeleteMapping("/unSignTastedCoffee")
    private R unSignTastedCoffee(@RequestBody CenterUserTasted userTasted) {
        QueryWrapper<CenterUserTasted> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userTasted.getUserId());
        wrapper.eq("coffee_id", userTasted.getCoffeeId());
        boolean remove = tastedService.remove(wrapper);
        if (remove) {
            return R.ok().message("取消标记成功");
        } else {
            return R.error().message("取消标记失败");
        }
    }

    @ApiOperation("获得用户喝过的咖啡的数量")
    @GetMapping("/getTasedCount/{userId}")
    private R getTastedNum(@PathVariable String userId){
        int count = tastedService.getCount(userId);
        return R.ok().data("count",count);
    }


}
